from jili.data.db import tradecal
import datetime
def __getmodel(rst,m,modelname,depth):
    for k ,v in m.__dict__.items():
        if k[0]!="_":
            if str(type(v))=="<class 'module'>":
                if depth>=0:
                    depth=depth-1
                    __getmodel(rst,v,modelname,depth)
            else:
                t={}
                if str(type(v)) == "<class 'function'>":
                    t["type"] = "function"
                elif str(type(v)) == "<class 'type'>":
                    t["type"] = "class"
                if t:
                    name=m.__name__
                    t["id"] = k
                    t["model"] = v
                    t["location"] = name
                    if modelname=="":
                        rst.update({k:t})
                    else:
                        if name.find(modelname)>=0:
                            rst.update({k:t})
def getmodels(modelname,depth=2):
    rst={}
    firstlist=modelname.split('.')
    if len(firstlist)>1:
        m=__import__(modelname,globals(), locals(), firstlist,level=0)
    else:
        m = __import__(modelname)
    __getmodel(rst,m,modelname,depth)
    return rst
def getobjs(k):
    objs=[]
    if "obj" in k.columns:
        objs=k["obj"]
        objs.drop_duplicates(inplace=True)
        objs=objs.to_list()
        return objs
def get_hgdays(date=None,days=1):
    if date  is None:
        date=datetime.datetime.now()
        date=datetime.datetime(date.year,date.month,date.day)
    # print(date)
    day1=tradecal.get_diff_tradeday(date,days)
    day1=day1-date
    day1=day1.days
    date=tradecal.getnexttradeday(date)
    date1=tradecal.get_diff_tradeday(date,diff=days)
    delta=date1-date
    day=delta.days
    return day,day1
def calc_1w(r,date=None,days=1):
    day=get_hgdays(date,days)
    return (100*day[0]*r)/365
def calc_1w_tlr(r0,r1,date=None,days=1):
    day = get_hgdays(date, days)
    pl0 = (100 * day[0] * r0) / 365
    pl1= (100 * day[0] * r1) / 365
    return pl1-pl0
def calc_r1k(r,date=None,days=1):
    day=get_hgdays(date,days)
    r=10*day[0]*r/365
    return r,r/day[1]
def calc_r1k_tlr(r0,r1,date=None,days=1):
    day = get_hgdays(date, days)
    pl0 = (100 * day[0] * r0) / 365
    pl1= (100 * day[0]* r1) / 365
    return pl1-pl0
if __name__=="__main__":
    print(calc_r1k(4))
    print(get_hgdays())
    # print(get_hgdays("20230927"))
    # print(get_hgdays("20230928"))
    # print(get_hgdays(days=3))
    # print(get_hgdays(days=5))
    # print(get_hgdays(days=7))